package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有部门");
       List<Dept> pageResult = deptService.pagelist();
        return Result.success(pageResult);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门id:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询部门id:{}",id);
       Dept dept = deptService.getById(id);
       return Result.success(dept);
    }
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }
    @GetMapping
    public Result page(@RequestParam (name = "name",required = false) String name,
                       @RequestParam (name = "status",required = false) Integer status,
                       @RequestParam(name ="page",required = true,defaultValue = "1") Integer page,
                       @RequestParam(name ="pagesize",required = true,defaultValue = "10") Integer pagesize){


        log.info("分页查询部门，参数：部门名称={},部门状态={},当前页码={},每页大小={}",name,status,page,pagesize);
        PageResult<Dept> pageResult = deptService.page(name,status,page,pagesize);
        return Result.success(pageResult);
    }

}

